﻿@model string           
<div class="commonFilters" style="text-align: left;">
    <span style="margin: 20px 10px;"><a id="weekFilter" class="filter" href="">Week</a></span>
    <span style="margin: 20px 10px;"><a id="monthFilter" class="filter" href="">Month</a></span>
    <span style="margin: 20px 10px;"><a id="todayFilter" class="filter" href="">Today</a></span>
</div>
<input id="currentFormId" value="@Model" type="hidden"/>
<table id="filterExtension">
    <tr>
        <td>
            <span>From:</span>
        </td>
        <td>
            <input id="fromDate" name="fromDate" type="text" value="@ViewBag.FromDate"/>
        </td>
    </tr>
    <tr>
        <td>
            <span>To:</span>
        </td>
        <td>
            <input id="toDate" name="toDate" type="text" value="@ViewBag.ToDate"/>
        </td>
    </tr>
</table>
<input type="submit" value="Filter" onclick="return submitFormWithFilter()" />
<br />
<br />
 <script type="text/javascript">   
     $(function () {
         var currentFormSelector = $('#currentFormId').val();
          currentForm = $(currentFormSelector);
            $('tfoot a').click(function () {
                
                var page = this.href.match(/page=([0-9]+)/)[1];
                var hiddenFromDate = $('<input type="hidden" name="fromDate" />');
                hiddenFromDate.attr('value', $('#fromDate').attr('value'));

                var hiddenToDate = $('<input type="hidden" name="toDate" />');
                hiddenToDate.attr('value', $('#toDate').attr('value'));

                var hiddenPage = $('<input type="hidden" name="page" />');
                hiddenPage.val(page);

                currentForm.append(hiddenFromDate);
                currentForm.append(hiddenToDate);
                currentForm.append(hiddenPage);
                currentForm.submit();

                return false;
            });

            $("#todayFilter").click(function () {
                var todayDate = new Date();
                var todayAsString = todayDate.getMonth() + 1 + "/" + todayDate.getDate() + "/" + todayDate.getFullYear();
                $("#fromDate").attr("value", todayAsString);
                $("#toDate").attr("value", todayAsString);
                return false;
            });

            $("#weekFilter").click(function () {
                var todayDate = new Date();
                var fromDate = new Date();
                fromDate.setMonth(todayDate.getMonth());
                fromDate.setDate(todayDate.getDate() - 7);

                var todayAsString = todayDate.getMonth() + 1 + "/" + todayDate.getDate() + "/" + todayDate.getFullYear();
                var fromAsString = fromDate.getMonth() + 1 + "/" + fromDate.getDate() + "/" + fromDate.getFullYear();

                $("#fromDate").attr("value", fromAsString);
                $("#toDate").attr("value", todayAsString);
                return false;
            });

            $("#monthFilter").click(function () {
                var todayDate = new Date();
                var fromDate = new Date();
                fromDate.setMonth(todayDate.getMonth() - 1);
                fromDate.setDate(todayDate.getDate());

                var todayAsString = todayDate.getMonth() + 1 + "/" + todayDate.getDate() + "/" + todayDate.getFullYear();
                var fromAsString = fromDate.getMonth() + 1 + "/" + fromDate.getDate() + "/" + fromDate.getFullYear();
                $("#fromDate").attr("value", fromAsString);
                $("#toDate").attr("value", todayAsString);
                return false;
            });

            $("#fromDate").datepicker();
            $("#toDate").datepicker();
        });

        function submitFormWithFilter() {
            var hiddenFromDate = $('<input type="hidden" name="fromDate" />');
            hiddenFromDate.attr('value', $('#fromDate').attr('value'));

            var hiddenToDate = $('<input type="hidden" name="toDate" />');
            hiddenToDate.attr('value', $('#toDate').attr('value'));

            currentForm.append(hiddenFromDate);
            currentForm.append(hiddenToDate);
            currentForm.submit();
            return false;
        }
        
    </script>
